package business;

import gitutil.GitUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import pojo.GitCount;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: git逻辑处理
 * @Author: xiaobing
 * @system name: 工作流引擎
 * @copyright：长安新生（深圳）金融投资有限公司
 * @Date: Created in  2018/9/26 11:03
 */
public class GitLogic {

    /**
     * @param localPath 下载已有仓库到本地路径
     * @param branch    git分支
     * @Author: xiaobing
     * @Description: 统计单个项目单个人当天commit数量及代码量
     * @Date: 2018/9/25
     */
    public static List<GitCount> gitLogCommit(String localPath, String branch) throws IOException, GitAPIException {
        // 定义数据及变量
        List<GitCount> list = new ArrayList<>();
        String authName = "";
        String date = "";
        String commitMessage = "";
        String usrCde = "";
        int addLine = 0;
        int removeLine = 0;

        //git仓库地址，获取commit提交日志，通过revWalk进行循环提取
        Git git = new Git(new FileRepository(localPath + "/.git"));
        Repository repository = git.getRepository();
        RevWalk revWalk = new RevWalk(repository);
        ObjectId commitId = repository.resolve(branch);
        revWalk.markStart(revWalk.parseCommit(commitId));

        List<RevCommit> commitList = new ArrayList<>();
        RevCommit commit1 = null;
        // 类型转化为commit对象进行循环提取数据
        for (RevCommit revCommit : revWalk) {
            // 当前版本哈希值
            commit1 = revCommit;
            // 当前版本meger过来父版本哈希值有几个
            int parentNum = revCommit.getParentCount();

            if(parentNum == 0) {
                break;
            }

            for (int i=0; i< parentNum; i++) {
                // 当前版本父类哈希
                RevCommit revCommitParent = revCommit.getParent(i);


                // 集合中只保存两个版本值，进行比较，统计出新增和删除行数
                if (commitList.size() == 2) {
                    commitList.clear();
                }
                // 往集合中添加提交版本
                commitList.add(revCommit);
                commitList.add(revCommitParent);

                // 控制版本，两个相邻版本进行文件对比
                Map<String, Object> map = GitUtil.gitdiff(commitList, localPath);
                addLine = addLine + (int) map.get("addSize");
                removeLine = removeLine + (int) map.get("subSize");
            }

            //
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.format(revCommit.getAuthorIdent().getWhen());
            authName = revCommit.getAuthorIdent().getName();
            commitMessage = revCommit.getFullMessage();
            usrCde = revCommit.getAuthorIdent().getEmailAddress();

            GitCount gitCount = new GitCount();
            gitCount.setEmailAddress(usrCde);
            gitCount.setAuthName(authName);
            gitCount.setCommitDate(date);
            gitCount.setCommitMessage(commitMessage);
            gitCount.setAddLine(addLine);
            gitCount.setRemoveLine(removeLine);
            if (commitMessage.contains("Merge")) {
                gitCount.setIsMerge("Y");
            } else {
                gitCount.setIsMerge("N");
            }
            list.add(gitCount);

            //System.out.println(usrCde + "-----" + authName + "----" + date + "----" + commitMessage + "新增：" + addLine + "删除：" + removeLine);

            addLine = 0;
            removeLine = 0;

        }

        // 统计初始化项目代码量
        int num = GitUtil.getAllFileLines(commit1, localPath);
        GitCount gitCount = new GitCount();
        gitCount.setEmailAddress(usrCde);
        gitCount.setAuthName(authName);
        gitCount.setCommitDate(date);
        gitCount.setCommitMessage(commitMessage);
        gitCount.setAddLine(num);
        gitCount.setRemoveLine(0);
        gitCount.setIsMerge("N");
        list.add(gitCount);

        return list;
    }

}
